package com.group9.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.group9.dto.Message;
import com.group9.dto.PageInfo;
import com.group9.pojo.CommentResponse;
import com.group9.pojo.CustomerInfo;
import com.group9.pojo.ResourceComment;
import com.group9.repository.CommentRepository;
import com.group9.service.CommentMessageServer;
import com.group9.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Lcouncillor on 2020/6/5.
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    CommentMessageServer commentMessageServer;

    @Override
    public Integer addFirstLevelComment(ResourceComment resourceComment) {
        return commentRepository.insertResourceComment(resourceComment);
    }

    @Override
    public Integer addSecondLevelComment(CommentResponse commentResponse) {
        ResourceComment resourceComment = commentRepository.selectResourceCommentById(commentResponse.getParentId());
        commentResponse.setReceiverId(resourceComment.getSenderId());
        commentResponse.setReceiverName(resourceComment.getSenderName());
        commentResponse.setCommentTime(new Date());
        commentResponse.setStatus("2");

        //添加二级评论commentResponse对象
        commentRepository.insertCommentResponse(commentResponse);
        //增加一级评论的回复id
        saveResourceCommentResponse(commentResponse.getParentId(),commentResponse.getId());
        return 1;
    }

    @Override
    public Integer addThirdLevelComment(CommentResponse commentResponse) {

        commentResponse.setCommentTime(new Date());
        commentResponse.setStatus("3");

        //添加三级评论commentResponse对象
        commentRepository.insertCommentResponse(commentResponse);
        //增加一级评论的回复id
        saveCommentResponseResponseId(commentResponse.getParentId(),commentResponse.getId());
        return 1;
    }

    @Override
    public PageInfo showFirstLevelCommentByIdAndLimit(Integer resourceId,Integer currentPage) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCurrentPage(currentPage);
        return commentRepository.selectByResourceIdAndPage(""+resourceId,pageInfo);
    }

    @Override
    public Integer saveFirstLevelCommentPic(String id, String picUrl) {
        return commentRepository.updateCommentPic(id, picUrl);
    }

    @Override
    public Integer saveCommentThumbUp(String id, String userId) {
        ResourceComment resourceComment = commentRepository.selectResourceCommentById(id);
        if (resourceComment == null){
            return 0;
        }

        //获取已赞信息
        Integer thumbUpAmount = resourceComment.getThumbUpAmount();
        String[] thumbUpers = resourceComment.getThumbUperId();
        //判断这次是点赞还是取消赞
        boolean thumbed = true;

        if (thumbUpAmount == null || thumbUpAmount == 0){
            //未有人点过赞,直接添加信息
            resourceComment.setThumbUpAmount(1);
            resourceComment.setThumbUperId(new String[]{userId});
            commentRepository.insertResourceComment(resourceComment);
            return 1;
        }else {
            //未点过赞，添加点赞者信息
            String[] thumbUperNew1 = new String[thumbUpAmount + 1];
            //点过赞，删减点赞信息
            String[] thumbUperNew2 = new String[thumbUpAmount - 1];
            for (int i = 0; i < thumbUpAmount; i++) {
                if (thumbUpers[i].equals(userId)) {
                    thumbed = false;
                    for (int j = 0; j < i; j++) {
                        thumbUperNew2[j] = thumbUpers[j];
                    }
                    continue;
                }
                if (thumbed) {
                    thumbUperNew1[i] = thumbUpers[i];
                } else {
                    thumbUperNew2[i - 1] = thumbUpers[i];
                }
            }

            if (thumbed) {
                //点赞
                thumbUperNew1[thumbUpAmount] = userId;
                resourceComment.setThumbUpAmount(thumbUpAmount + 1);
                resourceComment.setThumbUperId(thumbUperNew1);
                commentRepository.insertResourceComment(resourceComment);
                return 1;
            } else {
                //取消赞
                resourceComment.setThumbUpAmount(thumbUpAmount - 1);
                resourceComment.setThumbUperId(thumbUperNew2);
                commentRepository.insertResourceComment(resourceComment);
                return -1;
            }
        }
    }

    @Override
    public ResourceComment showResourceCommentById(String id) {
        return commentRepository.selectResourceCommentById(id);
    }

    @Override
    public Integer saveResourceCommentResponse(String id, String responseId) {
        ResourceComment resourceComment =  commentRepository.selectResourceCommentById(id);
        if (resourceComment.getResponseId() == null){
            String[] responseIds = new String[]{responseId};
            resourceComment.setResponseId(responseIds);
            commentRepository.insertResourceComment(resourceComment);
        }else {
            String[] responseIds = new String[resourceComment.getResponseId().length+1];
            for (int i=0; i<resourceComment.getResponseId().length; i++){
                responseIds[i] = resourceComment.getResponseId()[i];

            }
            responseIds[resourceComment.getResponseId().length] = responseId;
            resourceComment.setResponseId(responseIds);
            commentRepository.insertResourceComment(resourceComment);
        }
        return 1;
    }

    @Override
    public CommentResponse showCommentResponseById(String id) {
        return commentRepository.selectCommentResponseById(id);
    }

    @Override
    public Integer saveCommentResponseResponseId(String id, String responseId) {
        CommentResponse commentResponse =  commentRepository.selectCommentResponseById(id);
        if (commentResponse.getResponseId() == null){
            String[] responseIds = new String[]{responseId};
            commentResponse.setResponseId(responseIds);
            commentRepository.insertCommentResponse(commentResponse);
        }else {
            String[] responseIds = new String[commentResponse.getResponseId().length+1];
            for (int i=0; i<commentResponse.getResponseId().length; i++){
                responseIds[i] = commentResponse.getResponseId()[i];

            }
            responseIds[commentResponse.getResponseId().length] = responseId;
            commentResponse.setResponseId(responseIds);
            commentRepository.insertCommentResponse(commentResponse);
        }
        return 1;
    }

    @Override
    public Integer commentMessageAlert(String receiverId) {
        Message message = new Message();
        message.setUserId(receiverId);
        message.setMessage("comment");
        Object obj = JSONArray.toJSON(message);
        String json = obj.toString();

        commentMessageServer.onMessage(message.getUserId(), json);
        return 1;
    }


}
